//
//  PictureCollectView.swift
//  CJPWB
//
//  Created by 陈锦彭 on 16/3/19.
//  Copyright © 2016年 chenjinpeng. All rights reserved.
//

import UIKit
import SDWebImage

class PictureCollectView: UICollectionView {
    // MARK: - 属性
    //图片处理的collectView的约束
    @IBOutlet weak var picCollectViewHeightCons: NSLayoutConstraint!
    
    @IBOutlet weak var picCollectViewWidthCons: NSLayoutConstraint!
    var picUrls : [NSURL] = [NSURL](){
        didSet {
        //设置图片视图的大小
        let (pictureViewWidth, pictureViewHeight) = calculatePictureCollectionViewSize(picUrls.count)
        picCollectViewWidthCons.constant = pictureViewWidth
        picCollectViewHeightCons.constant = pictureViewHeight
        reloadData()
        }
    }
    
    //初始化视图
    override func awakeFromNib() {
        //设置数据源
        self.dataSource = self
       
    }

}

// MARK: - UICollectionViewDataSource数据源方法
extension PictureCollectView : UICollectionViewDataSource {
 
    func collectionView(collectionView: UICollectionView, numberOfItemsInSection section: Int) -> Int {
        return picUrls.count
    }
    
    func collectionView(collectionView: UICollectionView, cellForItemAtIndexPath indexPath: NSIndexPath) -> UICollectionViewCell {
        let cell = collectionView.dequeueReusableCellWithReuseIdentifier("pictureCell", forIndexPath: indexPath) as! PictureCollectViewCell
        cell.backgroundColor = UIColor.redColor()
        cell.picUrl = picUrls[indexPath.row]
        return cell
        }

}
// MARK: - 计算pictureView的高度
extension PictureCollectView {

    
    ///计算放图片的collectView的高度
     func calculatePictureCollectionViewSize(pictureCount : Int) -> (CGFloat, CGFloat) {
        //自定义布局
        let flowLayout = self.collectionViewLayout as! UICollectionViewFlowLayout
        let picCollectViewW = UIScreen.mainScreen().bounds.width - margin * 2
        //计算每个cell的宽高
        let cellHW = (UIScreen.mainScreen().bounds.width - margin * 2 - pegging * 2) / 3
        
        //1.没有图片的情况
        if pictureCount == 0 {
            return (0, 0)
        }
        
        //2.一张图片的情况
        if pictureCount == 1 {
            guard let url = picUrls.last else {
            return (0, 0)
            }
            let imageUrl = url.absoluteString
            let image = SDWebImageManager.sharedManager().imageCache.imageFromDiskCacheForKey(imageUrl)
            let imageW = image.size.width * 2
            let imageH = image.size.height * 2
        
            flowLayout.itemSize = CGSize(width: imageW, height: imageH)
            flowLayout.minimumInteritemSpacing = 0
            flowLayout.minimumLineSpacing = 0
            return (picCollectViewW, imageH)
        }
        //其他的布局
        flowLayout.itemSize = CGSize(width: cellHW, height: cellHW)
        flowLayout.minimumInteritemSpacing = pegging
        flowLayout.minimumLineSpacing = pegging

        //3.四张图片的情况
        if pictureCount == 4 {
            let picCollectViewWH = cellHW * 2 + pegging
            return (picCollectViewWH, picCollectViewWH)
        }
        //4.其他情况
        
        let row =  CGFloat((pictureCount - 1) / 3 + 1)
        let picCollectViewH = row * cellHW + (row - 1) * pegging
        return (picCollectViewW, picCollectViewH)
    }
}


// MARK: - collectionViewCell
class PictureCollectViewCell : UICollectionViewCell {
    // MARK: - 属性
    @IBOutlet weak var imageView: UIImageView!
    
    var picUrl : NSURL = NSURL(){
        didSet{
        imageView.sd_setImageWithURL(picUrl, placeholderImage: UIImage(named: "empty_picture"))
        }
    }
    
    

}
